package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author shy
 * @since 2021-05-22
 */
@Service
@Component
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try{
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20002,"添加失败");
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
//        // 查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);

        // 查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);

        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 封装所有一级分类
        for (int i = 0; i < oneSubjectsList.size(); i++) {
            EduSubject oSubject = oneSubjectsList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oSubject, oneSubject);
            finalSubjectList.add(oneSubject);
//            ArrayList<TwoSubject> finalTwoSubjects = new ArrayList<>();
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubjectsList.size(); j++) {
                EduSubject tSubject = twoSubjectsList.get(j);
                if (tSubject.getParentId().equals(oSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectList);
        }
        // 封装所有二级分类
        return finalSubjectList;

//        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
//        wrapperOne.eq("parent_id", "0");
//        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);
//
//        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
//        wrapperTwo.ne("parent_id", "0");
//        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);
//
//        List<OneSubject> finalSubjectList = new ArrayList<>();
//
//        for (int i = 0; i < oneSubjectsList.size(); i++) {
//            EduSubject eduSubject = oneSubjectsList.get(i);
//            OneSubject oneSubject = new OneSubject();
//            BeanUtils.copyProperties(eduSubject, oneSubject);
//            finalSubjectList.add(oneSubject);
//
//            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
//            for (int j = 0; j < twoSubjectsList.size(); j++) {
//                EduSubject tSubject = twoSubjectsList.get(j);
//                if (tSubject.getParentId().equals(eduSubject.getId())){
//                    TwoSubject twoSubject = new TwoSubject();
//                    BeanUtils.copyProperties(tSubject,twoSubject);
//                    twoFinalSubjectList.add(twoSubject);
//                }
//            }
//            oneSubject.setChildren(twoFinalSubjectList);
//        }
//        return finalSubjectList;
    }


}
